package com.jorado.etl.service;

import com.jorado.core.Result;
import com.jorado.core.utility.IOUtils;
import com.jorado.core.utility.StringUtils;
import com.jorado.event.EventClient;
import com.jorado.search.core.service.impl.ExportToFileImpl;
import com.jorado.search.core.service.impl.SolrExporter;
import com.jorado.search.core.util.SolrUtils;
import com.jorado.search.solr.client.SolrjOption;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.common.SolrDocument;
import org.springframework.context.annotation.Description;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Description("Solr热门职位类别职位title导出")
public final class SolrHJTEtlService extends ExportToFileImpl<SolrDocument> {

    private int total;
    private static final String DEFAULT_JOBTYPE = "6,115,131,116,122,257,11,924,45,105,237,302,299";
    private Map<String, List<String>> jobTypeMap;

    public SolrHJTEtlService(SolrjOption positionSolr, String distFile) {
        this(positionSolr, DEFAULT_JOBTYPE, distFile);
    }

    public SolrHJTEtlService(SolrjOption positionSolr, String jobType, String distFile) {
        super(new SolrExporter(positionSolr, getSolrQuery(jobType)), distFile);
        this.jobTypeMap = getJobTypeMap(jobType);
        this.setAfterFilter(this::after);
    }

    @Override
    protected void export(List<SolrDocument> dataList) {

        String jobTitle;
        String jobType;
        List<String> jobTypes;

        for (SolrDocument doc : dataList) {

            jobType = SolrUtils.getDocValue(doc, "JOB_TYPE");
            jobTitle = SolrUtils.getDocValue(doc, "JOB_TITLE");

            jobTypes = StringUtils.split(jobType, " ", true);

            for (String jt : jobTypes) {
                if (!jobTypeMap.containsKey(jt)) {
                    continue;
                }
                if (!jobTypeMap.get(jt).contains(jobTitle)) {
                    jobTypeMap.get(jt).add(jobTitle);
                    total++;
                }
            }
        }
    }

    protected Result after(Result result) {
        try {
            for (Map.Entry<String, List<String>> entry : jobTypeMap.entrySet()) {
                List<String> line = new ArrayList() {{
                    add(entry.getKey() + ":" + StringUtils.joinString(entry.getValue()));
                }};
                IOUtils.appendLines(distFile, line);
            }
            return Result.OK;
        } catch (Exception ex) {
            EventClient.getDefault().submitException(ex);
            return Result.ERROR;
        } finally {
            EventClient.getDefault().submitLog(String.format("共抽取热门类别职位:[%d]个", total));
        }
    }

    private static SolrQuery getSolrQuery(String jobType) {
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery("*:*");
        solrQuery.setFields("JOB_TYPE,JOB_TITLE");
        List<String> filterQuery = new ArrayList<>();
        for (Map.Entry<String, List<String>> entry : getJobTypeMap(jobType).entrySet()) {
            filterQuery.add("JOB_TYPE:" + entry.getKey());
        }
        String query = StringUtils.joinString(filterQuery, " OR ");
        solrQuery.addFilterQuery(query);
        solrQuery.setSort("DATE_CREATED", SolrQuery.ORDER.asc);
        return solrQuery;
    }

    private static Map<String, List<String>> getJobTypeMap(String jobType) {
        Map<String, List<String>> jobTypeMap = new HashMap<>();
        List<String> jobTypes = StringUtils.split(jobType, ",");
        for (String j : jobTypes) {
            jobTypeMap.put(j, new ArrayList<>());
            jobTypeMap.put(StringUtils.padLeft(j, 3, '0'), new ArrayList<>());
        }
        return jobTypeMap;
    }
}
